package bid.yanhj.common.util;

import java.util.HashMap;
import java.util.Map;

/**
 * 线程缓存类.
 * 
 * @author yanhaijiang
 *
 */
public class LocalMap {
	/**
	 * 线程本地临时存储数据.
	 */
	private static final ThreadLocal<Map<String, Object>> CONTEXT = new ThreadLocal<Map<String, Object>>();

	/**
	 * Constructor.
	 */
	private LocalMap() {

	}

	/**
	 * 资源释放.
	 */
	public static void destroy() {
		Map<String, Object> map = contextMap();
		if (map != null) {
			if (!map.isEmpty()) {
				map.clear();
			}
		}
		CONTEXT.remove();
	}

	/**
	 * get map.
	 * 
	 * @return Map
	 */
	public static Map<String, Object> contextMap() {
		Map<String, Object> map = CONTEXT.get();
		if (map == null) {
			map = new HashMap<String, Object>();
			CONTEXT.set(map);
		}
		return map;
	}
}
